<?php

$page = request("page");
$filePath = "../CodeBehind/" . $page . ".page.php";

if (file_exists($filePath)) 
{
    include_once $filePath;


    if (!isset($_SESSION)) {
        session_start();
    }

    if ($_FILES != null) // TODO
        ; 

    $json = json_decode(request("json"), true);
    $page = request("page"); //object to make
    $action = request("action"); //function to call withing object
    
    $ids = $json["ids"];  
    $formVals = $json["form"];
    $parameters = $json["parameters"];
    $get = $json["get"];
    $vars = $json["vars"]; //not used yet....prob won't ever be used

    $permission = true;
    if (makeNewObject($page))
    {
        $obj = new $page($ids, $formVals, $get);
        $permission = $obj->CheckSecurity(); //if making a new object, then check that they have permissions.
    }
    else 
    {
        $obj = $_SESSION["page"];
        $obj->initialize($ids, $formVals, $get);
    }

    if (method_exists($obj, $action)) 
    {
        if($permission) //only call $action() if the user had permission. 
        {
            DoAction($obj,$action,$parameters);
        }
        $jsonIds = $obj->EncodeIds();
        $_SESSION["page"] = $obj;
        echo $jsonIds;
    } 
    else
    {
        echo "Method: $action does not exist";
    }
} 
else 
{
    echo "File: $page does not exist";
}

/////// HELPER FUNCTIONS

/***
 *  Very greasy to call the action. There is a proper way to use actual reflection but I'm guessing
 *  this has a lower performance hit. 
 *  LIMITATION: Max of 5 parameters so far...
 */
function DoAction($obj,$action,$parameters)
{
    //$obj->$action();
    switch(count($parameters))
    {
        case 0:
            $obj->$action();
            break;
        case 1:
            $obj->$action($parameters[0]);
            break;
        case 2:
            $obj->$action($parameters[0],$parameters[1]);
            break;
        case 3:
            $obj->$action($parameters[0],$parameters[1],$parameters[2]);
            break;
        case 4:
            $obj->$action($parameters[0],$parameters[1],$parameters[2],$parameters[3]);
            break;
        case 5:
            $obj->$action($parameters[0],$parameters[1],$parameters[2],$parameters[3],$parameters[4]);
            break;
            
    }
}

function request($var) {
    if ($_REQUEST[$var] != null) {
        return $_REQUEST[$var];
    }
    else
        return '';
}

/**
 * Determines if need to make a new instance of the page Object
 * @param String $page
 * @return boolean : True if need to make new object. False if it already exists
 */
function makeNewObject($page) {
    if (!PageSet() || FirstLoad($page))
        return true;
    return false;
}

/**
 * Checks if this is the page's first load
 * @param String $page
 * @return boolean
 */
function FirstLoad($page) {
    if (strtolower($page) != strtolower(getClassName()))
        return true;
    return false;
}

/**
 * Checks if the page variable in SESSION is Set
 * @return boolean
 */
function PageSet() {
    if ($_SESSION["page"] == null)
        return false;
    return true;
}

function getClassName() {
    return get_class($_SESSION["page"]);
}

?>